﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using spotnumbers.Controller;
using spotnumbers.Model;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace spotnumbers
{
    public sealed partial class Board : UserControl
    {

        public Game game
        {
            set;
            get;
        }

        public Board()
        {
            this.InitializeComponent();
        }

        public void Update()
        {
            var radius = GameController.Radius;
            var controller = App.GameController;

            // clear all numbers
            enabledLayer.Children.Clear();
            disabledLayer.Children.Clear();
            textLayer.Children.Clear();

            var numbers = App.GameController.Game.Numbers;
            foreach (Number number in numbers)
            {
                var isNumberEnabled = controller.IsEnabled(number);
                var circle = new EllipseGeometry() { RadiusX = radius, RadiusY = radius };
                var x = number.X;
                var y = number.Y;
                circle.Center = new Point(x, y);
                if (isNumberEnabled)
                    enabledLayer.Children.Add(circle);
                else
                    disabledLayer.Children.Add(circle);

                var gridAroundText = new Grid() { Width = 2 * radius, Height = 2 * radius };
                //gridAroundText.Background = new SolidColorBrush(Colors.Red);
                textLayer.Children.Add(gridAroundText);
                Canvas.SetLeft(gridAroundText, x - radius);
                Canvas.SetTop(gridAroundText, y - radius);

                var textBlock = new TextBlock() { Text = number.Text, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, FontWeight = FontWeights.Bold, FontSize = GameController.Radius};
                if (!isNumberEnabled)
                    textBlock.Foreground = new SolidColorBrush(Colors.Gray);
                gridAroundText.Children.Add(textBlock);

            }
        }

        private void OnTapped(object sender, TappedRoutedEventArgs e)
        {
            Point point = e.GetPosition(canvas);
            if (point != null)
            {
                App.GameController.PlayerClicked(point.X, point.Y);
            }
        }
    }
}
